home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4512 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: ostrstream: Who knows it exectly?
  5. Date: 30 Jan 1996 18:57:19 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan30135719@g7240065.bridge.bst.bls.com>
  8. References: <4el56r$357@fsuj01.rz.uni-jena.de>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: mkt@isun04.inf.uni-jena.de's message of 30 Jan 1996 13:07:39 GMT
  11.  
  12. In article <4el56r$357@fsuj01.rz.uni-jena.de> mkt@isun04.inf.uni-jena.de (Tilo Koerbs) writes:
  13.  
  14. : Look at this piece of code:
  15.  
  16. : ostrstream buf;        // dynamic buffer.
  17. : // Fill buf...
  18. : buf << ends;        // Append NULL-byte.
  19. : char *p = buf.str();    // Freeze buffer.
  20.  
  21. : And now the problem: The users must delete the buffer
  22. : by there own. But HOW???
  23. : This way:    delete p;
  24. : Or this way:    delete [] p;
  25.  
  26. : Different books give different answers!
  27. : Is it implementation dependend?    (I don't think so!)
  28. : Or can I do both?        (I don't think so too!)
  29.  
  30. You must use:
  31.     delete[] p;
  32.  
  33. buf.str() returns a pointer to the array being used. Using delete and not
  34. delete[] on that array leads to undefined behaviour.
  35.  
  36. 5.3.5 Delete
  37. ...
  38. 2 In either alternative, if the value of the operand of the delete is the
  39.   null pointer the operation has no effect. Otherwise, in the first
  40.   alternative (delete object), the value of the operand of the delete shall
  41.   be a pointer to a non-array object created by [valid creations deleted for
  42.   brevity...]. If not the behaviour is undefinded. In the second alternative
  43.   (delete[] array), the value of the operand of the deletee shall be a pointer
  44.   to an array created by a new-expression without a new-placement
  45.   specification. If not, the behaviour is underfined.
  46.  
  47. Regards
  48.  
  49.   -A.
  50. -- 
  51. | A.Champion                |
  52.